package com.edu.boot.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edu.boot.common.api.CommonResult;
import com.edu.boot.entity.*;
import com.edu.boot.entity.Vo.*;
import com.edu.boot.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@Controller
public class RootBusinessManageController {
    @Autowired
    UserService merchantsUserService;
    @Autowired
    RecruitmentService recruitmentService;
    @Autowired
    BusinessShopService businessShopService;
    
    @Autowired
    NotesManageService notesManageService;
    @Autowired
    SResumeService sResumeService;

    /**
     * 兼职列表查询（企业和管理端）
     *
     * @return
     */
    @ResponseBody
    @GetMapping("shop/show")
    public CommonResult shopShow(@RequestParam(name = "currentpage") Integer current,
                                 @RequestParam(name = "pagesize") Integer size,
                                 @RequestParam(name = "recName", required = false) String recName,
                                 @RequestParam(name = "recType",required = false) String recType) {
        Page<Recruitment> page = new Page<>(current, size);
        Recruitment recruitment = new Recruitment();
        recruitment.setRecName(recName);
        recruitment.setRecType(StringUtils.isNotBlank(recType)?Integer.valueOf(recType):null);
        // 获取所有招聘信息
        IPage<Recruitment> recruitmentPage = businessShopService.getBusinessShopInfo(page,recruitment);
        List<Recruitment> recruitmentList = Collections.emptyList();
        if (CollectionUtil.isNotEmpty(recruitmentPage.getRecords())){
            recruitmentList = recruitmentPage.getRecords()
                    .stream()
                    .map(item -> {
                        String date = DateUtil.format(item.getCreateTime(), "yyyy-MM-dd: HH:mm:ss");
                        item.setCreateTime(DateUtil.parseDate(date));
                        return item;
                    })
                    .collect(Collectors.toList());
        }
        recruitmentPage.setRecords(recruitmentList);
        return CommonResult.success(recruitmentPage, "获取商家列表成功！！");
    }


    /**
     * 招聘管理列表
     *
     * @return
     */
    @ResponseBody
    @GetMapping("recruitment/show")
    public CommonResult recruitmentShow() {
        QueryWrapper<Recruitment> recruitmentQueryWrapper = new QueryWrapper<>();

        recruitmentQueryWrapper.select("rec_id", "rec_station", "rec_salary", "rec_heat", "rec_info", "rec_obj");
        List<Recruitment> list = recruitmentService.list(recruitmentQueryWrapper);
        if (list.isEmpty()) return CommonResult.failed("获取商家列表失败！！");
        return CommonResult.success(list, "获取商家列表成功！！");
    }



    
    

    @Autowired
    TopService topService;
    @Autowired
    BannerService bannerService;
    

    /**
     * 管理员通过nid删除某个招聘下的某个简历
     */
    @ResponseBody
    @PostMapping("rec/notes/del")
    public CommonResult delNotesByNid(@RequestParam(name = "nId") Integer nId) {
        QueryWrapper<NotesManage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("nid", nId);
        boolean remove = notesManageService.remove(queryWrapper);
        if (!remove) return CommonResult.failed("删除失败！！");

        return CommonResult.success(null, "删除成功！！");
    }


    @Autowired
    CommentService commentService;
    @Autowired
    CommentReplyService commentReplyService;

    /**
     * 获取所有招聘信息下的评论信息
     */
    @ResponseBody
    @GetMapping("rec/comment")
    public CommonResult getAllRecAndComment() {
        final List<CommentChildrenCommentReply> list = commentService.getAllCommentAndReply();
        for (CommentChildrenCommentReply comment : list) {
            final QueryWrapper<CommentReply> commentReplyQueryWrapper = new QueryWrapper<>();
            commentReplyQueryWrapper.eq("answer_id",comment.getComId());
            final List<CommentReply> list1 = commentReplyService.list(commentReplyQueryWrapper);
            comment.setChildren(list1);
        }
        if (list.isEmpty()) return CommonResult.failed("获取失败！！");
        return CommonResult.success(list, "获取成功！！");
    }

}
